package com.xssweibo.base;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;


/**
 * A data class representing Basic user information element
 */
public class User implements java.io.Serializable {

    static final String[] POSSIBLE_ROOT_NAMES = new String[]{"user", "sender", "recipient", "retweeting_user"};
    private int id;
    private String screenName;	// 用户昵称
    private String name;		// 好友显示昵称
    private int province;
    private int city;
    private String location;
    private String description;
    private String url;			// 用户博客地址
    private String profileImageUrl;
    private String profileUrl;    
    private String domain;
    private String weihao;		// 用户的微号
    private String gender;	    // 性别
    private int followersCount;
    private int friendsCount;
    private int statusesCount;
    private int favouritesCount;
    private String createdAt;
    private boolean following;
    private boolean allowAllActMsg;
    private boolean geoEnabled;
    private boolean verified;
    private int verifiedType;
    private String remark;
    private Status recentStatus;
    private boolean allowAllComment;
    private String avatarLarge;
    private boolean verifiedReason;
    private boolean followMe;
    private int onlineStatus;
    private int biFollowersCount;
    private String lang;

    private static final long serialVersionUID = -6345893237975349030L;

    User(JSONObject json) throws WeiboException {
        init(json);
    }

    private void init(JSONObject json) throws WeiboException {
    	if(json!=null){
        try {
            id = json.getInt("id");
            name = json.getString("name");
            screenName = json.getString("screen_name");
            location = json.getString("location");
            description = json.getString("description");
            profileImageUrl = json.getString("profile_image_url");
            url = json.getString("url");
            followersCount = json.getInt("followers_count");

            friendsCount = json.getInt("friends_count");
            createdAt = json.getString("created_at");
            favouritesCount = json.getInt("favourites_count");
            statusesCount = json.getInt("statuses_count");
            if (!json.isNull("status")) {
            	recentStatus = new Status(json.getJSONObject("status"));
            }
        } catch (JSONException jsone) {
            throw new WeiboException(jsone.getMessage() + ":" + json.toString(), jsone);
        }
    	}
    }


    /**
     * Returns the id of the user
     *
     * @return the id of the user
     */
    public int getId() {
        return id;
    }

    /**
     * Returns the name of the user
     *
     * @return the name of the user
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the screen name of the user
     *
     * @return the screen name of the user
     */
    public String getScreenName() {
        return screenName;
    }

    /**
     * Returns the location of the user
     *
     * @return the location of the user
     */
    public String getLocation() {
        return location;
    }

    /**
     * Returns the description of the user
     *
     * @return the description of the user
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns the profile image url of the user
     *
     * @return the profile image url of the user
     */
    public URL getProfileImageURL() {
        try {
            return new URL(profileImageUrl);
        } catch (MalformedURLException ex) {
            return null;
        }
    }

    /**
     * Returns the url of the user
     *
     * @return the url of the user
     */
    public URL getURL() {
        try {
            return new URL(url);
        } catch (MalformedURLException ex) {
            return null;
        }
    }


    /**
     * Returns the number of followers
     *
     * @return the number of followers
     * @since Weibo4J 1.0.4
     */
    public int getFollowersCount() {
        return followersCount;
    }

    public static List<User> constructUsers(String strJson) throws WeiboException {
    	try {
    		JSONObject object = new JSONObject(strJson);
            JSONArray list = object.getJSONArray("user");
            int size = list.length();
            List<User> users = new ArrayList<User>(size);
            for (int i = 0; i < size; i++) {
                users.add(new User(list.getJSONObject(i)));
            }
            return users;
        } catch (JSONException jsone) {
            throw new WeiboException(jsone);
        } catch (WeiboException te) {
            throw te;
        }  
    }
    

    public int getFriendsCount() {
        return friendsCount;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public int getFavouritesCount() {
        return favouritesCount;
    }


    /**
     *
     * @deprecated
     */
    public boolean isFollowing() {
        return following;
    }



    public int getStatusesCount() {
        return statusesCount;
    }

    /**
     * @return the user is enabling geo location
     * @since Weibo4J 2.0.10
     */
    public boolean isGeoEnabled() {
        return geoEnabled;
    }

    /**
     * @return returns true if the user is a verified celebrity
     * @since Weibo4J 2.0.10
     */
    public boolean isVerified() {
        return verified;
    }


    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        return obj instanceof User && ((User) obj).id == this.id;
    }

    @Override
    public String toString() {
        return "User{" +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", screenName='" + screenName + '\'' +
                ", location='" + location + '\'' +
                ", description='" + description + '\'' +
                ", profileImageUrl='" + profileImageUrl + '\'' +
                ", url='" + url + '\'' +
                ", followersCount=" + followersCount +
                ", friendsCount=" + friendsCount +
                ", createdAt=" + createdAt +
                ", favouritesCount=" + favouritesCount +
                ", following=" + following +
                ", statusesCount=" + statusesCount +
                ", geoEnabled=" + geoEnabled +
                ", verified=" + verified +
                '}';
    }
}
